package com.lottery.material.service.portal.user.impl;

import com.lottery.material.dao.article.ArticleActiveInfoMapper;
import com.lottery.material.dao.article.ArticleInfoMapper;
import com.lottery.material.dao.portaluser.CommentInfoMapper;
import com.lottery.material.dao.portaluser.UserLikeRecordMapper;
import com.lottery.material.pojo.article.ArticleInfo;
import com.lottery.material.pojo.portaluser.CommentInfo;
import com.lottery.material.pojo.portaluser.UserLikeRecord;
import com.lottery.material.service.portal.user.UserLikeRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * Description:    点赞/取消点赞服务实现类
 * Author:         sunny
 * CreateDate:     2019-04-15 09:58
 */
@Service
@Slf4j
public class UserLikeRecordServiceImpl implements UserLikeRecordService {

    @Autowired
    private UserLikeRecordMapper userLikeRecordMapper;
    @Autowired
    private ArticleInfoMapper articleInfoMapper;
    @Autowired
    private ArticleActiveInfoMapper articleActiveInfoMapper;
    @Autowired
    private CommentInfoMapper commentInfoMapper;

    /**
     * description: 删除用户点赞记录
     * author sunny
     * createDate 2019-04-15 上午 9:59
     *
     * @param userLikeRecord
     */
    @Override
    public Integer deleteUserLikeRecord(UserLikeRecord userLikeRecord) {
        UserLikeRecord like = userLikeRecordMapper.findByUserIdAndLikeTypeAndObjectId(userLikeRecord);
        if (Objects.isNull(like)) {
            log.info("当前用户：{}-->还没有点赞记录；点赞类型：{}-->文章/评论id：{}", userLikeRecord.getUserId(), userLikeRecord.getLikeType(), userLikeRecord.getObjectId());
            return -1;
        }

        Integer count = userLikeRecordMapper.deleteByPrimaryKey(like.getId());
        if (count > 0) {
            if(1 == userLikeRecord.getLikeType()){
                articleInfoMapper.updateLikeTotalByArticleId(userLikeRecord.getObjectId(), "1");
                articleActiveInfoMapper.updateLikeTotalByArticleId(userLikeRecord.getObjectId(), "1");
                ArticleInfo articleInfo = articleInfoMapper.findByArticleId(userLikeRecord.getObjectId());
                return articleInfo.getLikeTotal();
            }else{
                commentInfoMapper.updateLikeTotalByCommentId(userLikeRecord.getObjectId(), "1");
                CommentInfo commentInfo = commentInfoMapper.findByCommentId(userLikeRecord.getObjectId());
                return commentInfo.getLikeTotal();
            }
        } else {
            return -1;
        }
    }

    /**
     * description: 保存用户点赞记录
     * author sunny
     * createDate 2019-04-15 上午 10:02
     *
     * @param userLikeRecord
     */
    @Override
    public Integer saveUserLikeRecord(UserLikeRecord userLikeRecord) {
        UserLikeRecord like = userLikeRecordMapper.findByUserIdAndLikeTypeAndObjectId(userLikeRecord);
        if (Objects.nonNull(like)) {
            log.info("当前用户：{}-->已经存在点赞记录；点赞类型：{}-->文章/评论id：{}", userLikeRecord.getUserId(), userLikeRecord.getLikeType(), userLikeRecord.getObjectId());
            return -1;
        }

        Integer count = userLikeRecordMapper.insertSelective(userLikeRecord);
        if (count > 0) {
            if(1 == userLikeRecord.getLikeType()){
                articleInfoMapper.updateLikeTotalByArticleId(userLikeRecord.getObjectId(), "2");
                articleActiveInfoMapper.updateLikeTotalByArticleId(userLikeRecord.getObjectId(), "2");
                ArticleInfo articleInfo = articleInfoMapper.findByArticleId(userLikeRecord.getObjectId());
                return articleInfo.getLikeTotal();
            }else{
                commentInfoMapper.updateLikeTotalByCommentId(userLikeRecord.getObjectId(), "2");
                CommentInfo commentInfo = commentInfoMapper.findByCommentId(userLikeRecord.getObjectId());
                return commentInfo.getLikeTotal();
            }
        } else {
            return -1;
        }
    }

    /**
     * description: 根据点赞类型和对象Id查询点赞数
     * author sunny
     * createDate 2019-04-15 上午 10:37
     *
     * @param userLikeRecord
     * @return
     */
    @Override
    public Integer findCountByLikeTypeAndObjectId(UserLikeRecord userLikeRecord) {
        return userLikeRecordMapper.findCountByLikeTypeAndObjectId(userLikeRecord);
    }
}
